package com.example.hfut_edu;

import androidx.appcompat.app.AppCompatActivity;

import android.annotation.SuppressLint;
import android.app.Activity;
import android.content.Intent;
import android.content.SharedPreferences;
import android.os.Build;
import android.os.Bundle;
import android.webkit.JavascriptInterface;
import android.webkit.WebChromeClient;
import android.webkit.WebSettings;
import android.webkit.WebView;
import android.widget.Toast;

public class MainActivity extends AppCompatActivity {
    WebView webView_main;
    public static MainActivity main_obj = null;


    @SuppressLint({"SetJavaScriptEnabled", "JavascriptInterface"})
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        main_obj = this;

        webView_main = (WebView) findViewById(R.id.main_webview);
        WebSettings webSettings = webView_main.getSettings();
        webSettings.setJavaScriptEnabled(true);

        webView_main.getSettings().setDomStorageEnabled(true);
        String appCachePath = getApplicationContext().getCacheDir().getAbsolutePath();
        webView_main.getSettings().setAppCachePath(appCachePath);
        webView_main.getSettings().setAllowFileAccess(true);
        webView_main.getSettings().setAppCacheEnabled(true);
        if(Build.VERSION.SDK_INT>=Build.VERSION_CODES.LOLLIPOP) {
            webView_main.getSettings().setMixedContentMode(WebSettings.MIXED_CONTENT_ALWAYS_ALLOW);
        }
        //将一个java类对象传递到HTML页面中，
        webView_main.addJavascriptInterface(this, "JSBridge");
        webView_main.setWebChromeClient(new WebChromeClient());
        webView_main.loadUrl("file:///android_asset/www/main.html");

        try {
            //MainActivity.main_obj.finish();
            page_curriculum.curriculum_obj.finish();
            page_settings.settings_obj.finish();
            page_login.login_obj.finish();
            page_login_xxmh.login_xxmh_obj.finish();
            page_loadurl.loadurl_obj.finish();
        }catch (Exception e){
            //Toast.makeText(this, e.toString(), Toast.LENGTH_SHORT).show();
        }
    }


    @JavascriptInterface
    public void switch_to_page(final String contentStr) {//js->原生
        //Log.d("TAG", "contentStr:" + contentStr);
        runOnUiThread(new Runnable() {
            @Override
            public void run() {
                Intent intent;
                switch (contentStr){
                    case "shuji":
                        intent=new Intent(MainActivity.this, page_home.class);
                        startActivity(intent);
                        break;
                    case "settings":
                        intent=new Intent(MainActivity.this, page_settings.class);
                        startActivity(intent);
                        break;
                    case "kebiao":
                        intent=new Intent(MainActivity.this, page_curriculum.class);
                        startActivity(intent);
                        break;
                    case "daka":
                        intent=new Intent(MainActivity.this, page_daka.class);
                        //intent.putExtra("extra_data","https:iou.ink/daka/");
                        startActivity(intent);
                        break;
                    case "yikatong":
                        intent=new Intent(MainActivity.this, page_loadurl.class);
                        intent.putExtra("extra_data","http://155.94.158.151:23003/");
                        startActivity(intent);
                        //https://cas.hfut.edu.cn/cas/login?service=http%3A%2F%2F172.31.248.20%2FahdxdrPortalHome.action
                        break;
                    case "fuwu":
                        Toast.makeText(MainActivity.this, "功能尚未开放", Toast.LENGTH_SHORT).show();
                        break;
                    case "xxmh":
                        intent=new Intent(MainActivity.this, page_loadurl.class);
                        intent.putExtra("extra_data","https://cas.hfut.edu.cn/cas/login");
                        startActivity(intent);
                        break;
                    case "jwxt":
                        intent=new Intent(MainActivity.this, page_loadurl.class);
                        intent.putExtra("extra_data","http://jxglstu.hfut.edu.cn/eams5-student/home");
                        startActivity(intent);
                        break;
                    default:
                        break;
                }
            }
        });
    }


    @JavascriptInterface
    public void toaststh(final String contentStr) {//js->原生
        //Log.d("TAG", "contentStr:" + contentStr);
        runOnUiThread(new Runnable() {
            @Override
            public void run() {
                Toast.makeText(MainActivity.this, "教sss", Toast.LENGTH_SHORT).show();
            }
        });
    }


    public String rwSPdata(String flag,String key,String value){
        //email,passwd,vrcode
        SharedPreferences testSP= getSharedPreferences("testSP", Activity.MODE_PRIVATE);
        switch (flag){
            case "w":
                SharedPreferences.Editor editor = testSP.edit();
                editor.putString( key, value );
                editor.apply();
                return "";
            case "r":
                return testSP.getString( key, "" );
            default:
                return "";
        }
    }

    @JavascriptInterface
    public void setHtml_title() {//js->原生->js
        runOnUiThread(new Runnable() {
            @Override
            public void run() {
                //读取本地SD里储存的账号密码，向服务器请求信息
                //读取信息并赋值
                //Toast.makeText(MainActivity.this, "proing", Toast.LENGTH_SHORT).show();
                try{
                    String jsonstr=rwSPdata("r","userdata","");
                    String[] jsonlist = new String[26];
                    String jsonstr1=jsonstr.replace("{","").replace("}","").replace("[","").replace("]","").replace("'","");
                    jsonlist=jsonstr1.split(",");
                    String username=jsonlist[1].split(":")[1];
                    //webView_main.loadUrl("javascript:settitle('"+username+"')");
                    //webView_main.loadUrl("javascript:settitle("+username+")");
                    webView_main.loadUrl("javascript:settitle('"+username+"')");


                }catch (Exception e){
                    //Toast.makeText(MainActivity.this, e.toString(), Toast.LENGTH_SHORT).show();
                }

            }
        });
    }


    @JavascriptInterface
    public void getToken() {//js->原生->js，单独调用token
        runOnUiThread(new Runnable() {
            @Override
            public void run() {
                webView_main.loadUrl("javascript:getToken()");
            }
        });
    }


}